package com.spzx.product.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spzx.common.core.exception.ServiceException;
import com.spzx.product.api.domain.vo.Brand;
import com.spzx.product.domain.CategoryBrand;
import com.spzx.product.mapper.CategoryBrandMapper;
import com.spzx.product.service.ICategoryBrandService;
import com.spzx.product.service.ICategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CategoryBrandServiceImpl extends ServiceImpl<CategoryBrandMapper,CategoryBrand> implements ICategoryBrandService {
@Autowired
    ICategoryService categoryService;
@Autowired
CategoryBrandMapper categoryBrandMapper;

    @Override
    public List<CategoryBrand> selectCategoryBrandList(CategoryBrand categoryBrand) {
        return baseMapper.selectCategoryBrandList(categoryBrand);
    }

    @Override
    public CategoryBrand selectCategoryBrandById(Long id) {
        CategoryBrand categoryBrand = baseMapper.selectById(id);
        Long category3Id = categoryBrand.getCategoryId();
       List<Long>categoryIds = categoryService.getParentIds(category3Id);
//       List<Long>resultIds = new ArrayList<>();
//        for (Long categoryId:categoryIds
//             ) {
//            resultIds.add(0,categoryId);
//
//        }
        categoryBrand.setCategoryIdList(categoryIds);
        return categoryBrand;
    }

    @Override
    public int insertCategoryBrand(CategoryBrand categoryBrand) {
        Long count = baseMapper.selectCount(new LambdaQueryWrapper<CategoryBrand>()
                .eq(CategoryBrand::getCategoryId, categoryBrand.getCategoryId())
                .eq(CategoryBrand::getBrandId, categoryBrand.getBrandId()));
        if (count>0){
            throw new ServiceException("该分类已经添加该品牌");
        }
        return baseMapper.insert(categoryBrand);
    }

    @Override
    public int updateCategoryBrand(CategoryBrand categoryBrand) {
        CategoryBrand originalCategoryBrand = this.getById(categoryBrand.getId());
        if (categoryBrand.getCategoryId().longValue() !=originalCategoryBrand.getCategoryId().longValue()
        ||categoryBrand.getBrandId().longValue()!=originalCategoryBrand.getBrandId().longValue()){
            Long count = baseMapper.selectCount(new LambdaQueryWrapper<CategoryBrand>()
                    .eq(CategoryBrand::getCategoryId, categoryBrand.getCategoryId())
                    .eq(CategoryBrand::getBrandId, categoryBrand.getBrandId()));
            if (count>0){
                throw new ServiceException("该分类已经添加该品牌");
            }
        }
        return baseMapper.updateById(categoryBrand);
    }

    @Override
    public List<Brand> selectBrandListByCategoryId(Long categoryId) {
   List<Brand>brands= baseMapper.selectBrandListByCategoryId(categoryId);
        return brands;
    }


}
